package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.invoke.LambdaConversionException;
import java.util.List;

/**
 * 分类管理
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> addCategory(@RequestBody Category category){
        log.info("新增分类...category:{}",category);
        //补全数据
        //调用service层方法
        categoryService.save(category);
        return R.success("新增分类成功");
    }

    /**
     * 分类分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> pageCategory(Integer page,Integer pageSize){
        log.info("分类管理的分页查询... page:{}, pageSize:{}",page,pageSize);
        //构建分页构造器
        Page pageInfo = new Page(page,pageSize);
        //构建查询条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //构建查询条件
        //构造排序条件
        queryWrapper.orderByAsc(Category::getSort);
        //调用service
        categoryService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }

    /**
     * 删除分类信息
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> remove(Long ids){
        log.info("删除分类...ids{}",ids);
        //删除分类时要判断此分类下有没有菜品数据，如果有则提示信息
        //调用service
        //categoryService.removeById(ids);
        categoryService.removeByCategoryId(ids);
        return R.success("删除分类成功");
    }

    /**
     * 修改分类信息
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        log.info("修改分类信息:category:{}",category);
        categoryService.updateById(category);
        return R.success("修改分类成功");
    }

    /**
     * 增加菜品或套餐时的分类下拉列表
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        log.info("分类管理下拉列表。。。。。");
        //构建条件查询器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        //调用service层
        List<Category> list = categoryService.list(queryWrapper);
        return R.success(list);
    }

}
